package com.yugao.fintech.draper.hazelcast.config;

import com.hazelcast.core.HazelcastInstance;
import com.yugao.fintech.draper.hazelcast.mq.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.List;

@AutoConfiguration
public class HazelcastMessageAutoConfiguration {

	@Bean
	HazelcastMQTemplate hazelcastMQTemplate(@Qualifier("hazelcastInstance") HazelcastInstance hazelcastInstance,
			List<HazelcastMessageInterceptor> interceptors) {
		return new HazelcastMQTemplate(hazelcastInstance, interceptors);
	}

	@Bean
	HazelcastMessageListenerProvider hazelcastMessageListenerProvider(
			@Qualifier("hazelcastInstance") HazelcastInstance hazelcastInstance,
			List<AbstractTopicMessageListener<?>> topicMessageListeners,
			List<AbstractGroupMessageListener<?>> groupMessageListeners) {
		HazelcastMessageListenerProvider hazelcastMessageListenerProvider = new HazelcastMessageListenerProvider(
				hazelcastInstance);
		topicMessageListeners.forEach(hazelcastMessageListenerProvider::register);
		groupMessageListeners.forEach(hazelcastMessageListenerProvider::register);
		return hazelcastMessageListenerProvider;
	}
}
